﻿using CoreShop.Library;
using CoreShop.PrintService;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIMenuItem : UIUserControl
    {
        public MenuItemInfo Info { get; }

        public UIMenuItem()
        {
            InitializeComponent();

            Info = new MenuItemInfo();
            Text = Info.Name;
        }

        public UIMenuItem(MenuItemInfo info)
        {
            InitializeComponent();
            Info = info;
            Text = info.Name;
            ForeColor = info.ForeColor;
        }

        private bool selected = false;
        public bool Selected
        {
            get => selected;
            set
            {
                selected = value;
                Invalidate();
            }
        }

        private Color selectedColor = Color.FromArgb(221, 221, 221);
        public Color SelectedColor
        {
            get => selectedColor;
            set
            {
                selectedColor = value;
                Invalidate();
            }
        }

        private int itemHeight = 64;

        public int ItemHeight
        {
            get => itemHeight;
            set
            {
                itemHeight = value;
                Invalidate();
            }
        }

        public void SetMinSize()
        {
            ItemHeight = 70;//30
            Width = 370;
            Font = new Font("微软雅黑", 10.5f);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (Selected)
            {
                e.Graphics.FillRectangle(SelectedColor, 0, 0, Width, Height);
            }

            if (Info == null) return;
            string text;
            int row1_y = 0;//第一行
            int row2_y = itemHeight / 2;//第一行

            if (Info.RowId.IsValid())
            {
                e.Graphics.FillRoundRectangle(Color.DarkGray, new Rectangle(2, row1_y + 6, 21, 20), 5);
                using (Font font = new Font("微软雅黑", 10.5f))
                    e.Graphics.DrawString(Info.RowId, font, Color.White, new Rectangle(1, 0, Width, row2_y), ContentAlignment.MiddleLeft);
                //e.Graphics.DrawRoundRectangle(Color.FromArgb(255, 186, 33), new Rectangle(2, row1_y + 6, 21, 20), 5);
            }

            int returningWidth = 0;
            if (Info.IsReturning)
            {
                returningWidth = 22;
                e.Graphics.FillRoundRectangle(Color.FromArgb(38, 140, 229), new Rectangle(31, row1_y + 8, 21, 20), 5);
                using (Font font = new Font("微软雅黑", 10.5f))
                    e.Graphics.DrawString("退", font, Color.White, new Rectangle(30, 0, Width, row2_y), ContentAlignment.MiddleLeft);
            }

            //int byteCount = System.Text.Encoding.UTF8.GetByteCount(Text);
            //if (byteCount > 46)
            //{
            //    Text = CommFunc.CutString(Text,46);
            //}

            //第二行、名称
            Size namesize = TextRenderer.MeasureText(Text, Font);
            e.Graphics.DrawString(Text, Font, ForeColor, new Rectangle(26 + returningWidth, 0, Width, row2_y), ContentAlignment.MiddleLeft);
            if ((Info.IsPresent) && (!Info.IsReturning))
            {
                using (Font font = new Font("微软雅黑", 10.5f))
                    e.Graphics.DrawString("赠", font, Color.Red, new Rectangle(28 + namesize.Width + returningWidth, 0, Width, ItemHeight / 2), ContentAlignment.MiddleLeft);

                e.Graphics.DrawRoundRectangle(Color.FromArgb(255, 186, 33), new Rectangle(30 - 1 + namesize.Width, ItemHeight / 3 - 15, 21, 20), 5);
            }

            //第二行、单价
            if (Info.RealPrice.IsValid())
            {
                string priceText = "";
                if (CommFunc.ConvertToDecimal(Info.RealPrice) != CommFunc.ConvertToDecimal(Info.Price)) 
                    priceText = "原价:" + Info.Price;

                var priceFont = new Font("微软雅黑", 10f, System.Drawing.FontStyle.Strikeout);
                Size pricesize = TextRenderer.MeasureText(priceText, priceFont);
                e.Graphics.DrawString(priceText, priceFont, Color.Gray, new Rectangle(26, row2_y - 2, Width / 2, row2_y), ContentAlignment.MiddleLeft);

                text = "￥" + Info.RealPrice + "/" + Info.UnitName;
                using (Font font = new Font("微软雅黑", 10f))
                {
                    e.Graphics.DrawString(text, font, Color.Gray, new Rectangle(26 + pricesize.Width, row2_y - 2, Width / 2, row2_y), ContentAlignment.MiddleLeft);
                }
            }

            //第二行、数量+单位
            if (Info.Count.IsValid())
            {
                text = Info.Count + Info.UnitName;
                e.Graphics.DrawString(text, Font, ForeColor, new Rectangle(Width - 140 + 4, row2_y, Width, row2_y), ContentAlignment.MiddleLeft);
                //if (Info.Discounts.IsNullOrEmpty())
                //{
                //    e.Graphics.DrawString(text, Font, ForeColor, new Rectangle(Width - 120 + 4, row2_y, Width, row2_y), ContentAlignment.MiddleLeft);
                //}
                //string priceName = Info.Price + "*";
                //Size priceSize = TextRenderer.MeasureText(priceName, Font);
                //e.Graphics.DrawString(priceName, Font, Color.Gray, new Rectangle(Width / 2, row2_y, Width, row2_y), ContentAlignment.MiddleLeft);
                //if (Info.Discounts.IsNullOrEmpty())
                //{
                //    e.Graphics.DrawString(Info.Count, Font, ForeColor, new Rectangle(Width - 120 + 4, row2_y, Width, row2_y), ContentAlignment.MiddleLeft);
                //}
                //else
                //{
                //    if (Info.Money.IsValid())
                //        e.Graphics.DrawString("￥" + Info.Money, Font, Color.Red, new Rectangle(0, row2_y, Width, row2_y / 2), ContentAlignment.MiddleRight);

                //    if (Info.Discounts.IsValid())
                //    {
                //        using (Font font = new Font("微软雅黑", 9f, System.Drawing.FontStyle.Strikeout))
                //        {
                //            e.Graphics.DrawString("￥" + Info.Discounts, font, Color.Gray, new Rectangle(0, row2_y, Width, 20), ContentAlignment.MiddleRight);
                //        }
                //    }
                //}                
            }

            //金额
            if (Info.Money.IsValid())
                e.Graphics.DrawString("￥" + Info.Money, Font, Color.Tomato, new Rectangle(0, row2_y, Width, row2_y), ContentAlignment.MiddleRight);
                        
            int top = ItemHeight;
            if (Info.Tags.Count > 0)
            {
                top = ItemHeight - 8;
                using (Font font = new Font("微软雅黑", 10.5f))
                {
                    foreach (var item in Info.Tags)
                    {
                        Size size = new Size(Width - 35, ((int)e.Graphics.MeasureString(item, font, Width - 35, StringFormat.GenericTypographic).Height));
                        e.Graphics.DrawString(item, font, Brushes.Gray, new RectangleF(28, top, Width - 35, size.Height));
                        top += size.Height;
                        top += 2;
                    }
                }
                top += 2;
            }

            if (Height != top) Height = top;
        }

        private void UIMenuItem_Click(object sender, System.EventArgs e)
        {
            //Selected = !selected;
            Selected = true;
            foreach (var item in Parent.GetControls<UIMenuItem>())
            {
                if (item.Equals(this)) continue;

                item.Selected = false;
            }
        }
    }
}
